﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using imaginecup.util;
using Microsoft.Xna.Framework;
using Imaginecup.sprite.animation;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util.resourceMan;

namespace Imaginecup.sprite
{
    public class AnimationSprite:ISprite
    {
        private String m_TextureName;        
        private Vector2 m_FrameSize;

        private IAnimationIndexProvider m_AnimationIndex;

        private IReverseInfoClass m_ReverseInfo = null;
        private bool m_bAdditivAble;

        public AnimationSprite(IAnimationIndexProvider animationIndex)
        {
            m_AnimationIndex = animationIndex;
            m_bAdditivAble = false;
        }
        public AnimationSprite(IAnimationIndexProvider animationIndex , bool Addtive)
        {
            m_AnimationIndex = animationIndex;
            m_bAdditivAble = Addtive;
        }  

        public AnimationSprite(IAnimationIndexProvider animationIndex , IReverseInfoClass reverse)
        {
            m_ReverseInfo = reverse;
            m_AnimationIndex = animationIndex;
        }
        public void SetTextureName( String textureName )
        {
            m_TextureName = textureName;
            SetFrameSize();
        }

        private void SetFrameSize()
        {
            m_FrameSize = ResourceMan.GetResourceMan().GetFrameSize(m_TextureName);
        }

        public void Draw(SpriteBatch spriteBatch, EntityInfo entityInfo)
        {
            Texture2D texture = ResourceMan.GetResourceMan().GetTexture(m_TextureName);

            Vector2 indexV = m_AnimationIndex.GetAnimationIndex();


            if (m_bAdditivAble)
                spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
            else
                spriteBatch.Begin();

            
            
            if( m_ReverseInfo != null && m_ReverseInfo.GetReverseInfo())
            {
                spriteBatch.Draw(texture, new Rectangle((int)entityInfo.mPosition.X, (int)entityInfo.mPosition.Y,
                                                        (int)entityInfo.mSize.X, (int)entityInfo.mSize.Y),
                                         new Rectangle(
                                             (int)(indexV.X * m_FrameSize.X), (int)(indexV.Y * m_FrameSize.Y),
                                             (int)m_FrameSize.X, (int)m_FrameSize.Y
                                         ),
                               Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
            }
            
            else
            {
                spriteBatch.Draw(texture, new Rectangle((int)entityInfo.mPosition.X, (int)entityInfo.mPosition.Y,
                                                 (int)entityInfo.mSize.X, (int)entityInfo.mSize.Y),
                                 new Rectangle(
                                     (int)(indexV.X * m_FrameSize.X), (int)(indexV.Y * m_FrameSize.Y),
                                     (int)m_FrameSize.X, (int)m_FrameSize.Y
                                 ),
                     Color.White);
            }
         

            spriteBatch.End();
        }

        public void Release()
        {

        }

    }
}
